//
//  WBStatusListViewModel.swift
//  传智微博
//
//  Created by FYXJ（6） on 2018/9/5.
//  Copyright © 2018年 FYXJ（6）. All rights reserved.
//

import Foundation

/// 数据列表视图模型
class WBStatusListViewModel {
   
    
    lazy var statusList = [WBStatus]()
    
    
    func loadStatus(pullUP: Bool, completion: @escaping (_ isSuccess:Bool)->()) {
        
        let since_id = pullUP ? 0 : (statusList.first?.id ?? 0)
        
        let max_id = pullUP ? (statusList.last?.id ?? 0) : 0
        WBNetworkManager.shared.statusList (since_id: since_id, max_id: max_id) { (list, isSucc) in
            
            
//            guard let arry = list else {
//
//                return;
//            }
//
//            for dic in arry
//            {
//                let status = WBStatus()
//                status.id = dic["id"] as! Int64
//                status.text = dic["text"] as? String
//                self.statusList.append(status)
//            }
//            completion(true)
//            print("\(arry)")

         
        guard var array = NSArray.yy_modelArray(with: WBStatus.self, json: list ?? []) as? [WBStatus] else {
                completion(false)
                return
            }
            print("刷新的\(array.count)微博")
            print("每次请求的第一条微博的内容\(String(describing: array.first?.text))")
            print("每次请求的第一条微博的ID\(String(describing: array.first?.id))")
            if pullUP
            {
                array.removeFirst()
              self.statusList += array 
            }
            else
            {
                self.statusList = array + self.statusList
            }
            completion(true)
        }
    }
}
